//
//  RGInfoEditVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/21.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGInfoEditVC.h"
#import "RGMeInterfaceBulider.h"
@interface RGInfoEditVC ()<UITextFieldDelegate>

@property (nonatomic,copy) NSString *titleStr;
@property (nonatomic,copy) NSString *placeholder;
@property (nonatomic,assign) NSInteger maxLength;

@property (nonatomic,strong) UILabel *tileLabel;
@property (nonatomic,strong) UILabel *numLabel;
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIView *line;
@end

@implementation RGInfoEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showBackButton];
    [self showRightButtonWithTitle:@"保存" target:self selector:@selector(save:)];
    self.customContentView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
    [self.customContentView addSubview:self.tileLabel];
    [self.customContentView addSubview:self.textField];
    [self.customContentView addSubview:self.line];
    [self.customContentView addSubview:self.numLabel];
    
    [_tileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.top.mas_equalTo(20);
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.tileLabel.mas_leading);
        make.top.equalTo(self.tileLabel.mas_bottom).offset(20);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.tileLabel.mas_leading);
        make.trailing.mas_equalTo(-20);
        make.top.equalTo(self.textField.mas_bottom).offset(15);
        make.height.mas_equalTo(0.8);
    }];
    
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.tileLabel.mas_leading);
        make.top.equalTo(self.line.mas_bottom).offset(15);
    }];
    
    switch (self.editType) {
        case NickName: {
            self.customNavBar.title = @"昵称";
            self.titleStr = @"我的昵称";
            self.placeholder = @"请填写昵称";
            self.maxLength = 20;
        }
            break;
        case UserComment: {
            self.customNavBar.title = @"修改签名";
            self.titleStr = @"个性签名";
            self.placeholder = @"填写个性签名更容易获得别人关注哦";
            self.maxLength = 40;
        }
            break;
        case School: {
            self.customNavBar.title = @"学校";
            self.titleStr = @"我的学校";
            self.placeholder = @"请填写学校";
            self.maxLength = 20;
        }
            break;
        default:
            break;
    }
    _numLabel.text = [NSString stringWithFormat:@"0/%d",self.maxLength];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:
                                      @{NSForegroundColorAttributeName:UICOLOR_WITH_RGBINT(0x999999),
                                        NSFontAttributeName:FONT(customFontName, 15)
                                        }];
    _textField.attributedPlaceholder = attrString;
    _tileLabel.text = self.titleStr;

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *newText = [NSMutableString stringWithString:textField.text];
    [newText replaceCharactersInRange:range withString:string];
    NSInteger strLength = textField.text.length - range.length + string.length;
    self.numLabel.text = [NSString stringWithFormat:@"%d/%d",strLength,self.maxLength];
    return (strLength < self.maxLength);
}

//在这个代理方法监测长度，超过受限长度则截取
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text.length > self.maxLength) {
        textField.text = [textField.text substringToIndex:self.maxLength];
    }else if(textField.text.length == 0) {
    }else {
    }
}

- (void)save:(UIButton *)btn {
//    HttpRequest *quest = [HttpRequest buildRequest:url_editInfo];
//    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
//    switch (self.editType) {
//        case NickName: {
//            user.nickname = self.textField.text;
//        }
//            break;
//        case UserComment: {
//            user.userComment = self.textField.text;
//        }
//            break;
//        case School: {
//            user.schoolName = self.textField.text;
//        }
//            break;
//        default:
//            break;
//    }
    
    if(self.userInfoChangedBlock){
        self.userInfoChangedBlock(_textField.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
//    NSDictionary *params = [RGMeInterfaceBulider buildEditInfo:user];
//    AA_WEAK_MAKE(self) weakSelf = self;
//    [quest post:params success:^(id data) {
//        [RGUserInfoModel storeUserInfo:user];
//        [weakSelf dismissLoading];
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//        [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationUserInfoChangedSuccess object:nil];
//    } fail:^(NSInteger code, NSString *message) {
//        [weakSelf showTextMessageWithMessage:message];
//    }];
}

- (UILabel *)tileLabel {
    if (!_tileLabel) {
        _tileLabel = [[UILabel alloc] init];
        _tileLabel.font = FONT(customFontName, 17);
        _tileLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
    }
    return _tileLabel;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.delegate = self;
        _textField.textColor = UICOLOR_WITH_RGBINT(0xffffff);
    }
    return _textField;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UICOLOR_WITH_RGBINT(0x999999);
    }
    return _line;
}

- (UILabel *)numLabel {
    if (!_numLabel) {
        _numLabel = [[UILabel alloc] init];
        _numLabel.font = FONT(customFontName, 17);
        _numLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
    }
    return _numLabel;
}

@end
